#include "oss_pkg_add.h"
#include "ui_oss_pkg_add.h"

oss_pkg_add::oss_pkg_add(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::oss_pkg_add)
{
    ui->setupUi(this);
}

oss_pkg_add::~oss_pkg_add()
{
    delete ui;
}

void oss_pkg_add::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void oss_pkg_add::creat_xml()
{
    QFile file(ui->xml_file->text());
    if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
    {
        return;
    }
    QTextStream outStream(&file);

    QDomDocument doc;
    QDomNode node = doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\" ");
    doc.insertBefore(node, doc.firstChild());
    QDomElement pkgs = doc.createElement("pkgs");
    doc.appendChild(pkgs);
    doc.save(outStream,4);
    file.close();
}

void oss_pkg_add::on_xml_read_clicked()
{
    QFile file(ui->xml_file->text());
    if(!file.open(QIODevice::ReadOnly))
    {
        creat_xml();
        if(!file.open(QIODevice::ReadOnly))
        {
            QMessageBox msgBox;
            msgBox.setText("Xml既无法读取也无法写入.");
            msgBox.exec();
            doc.clear();
            return;
        }
    }

    QString strError;
    int errLine, errColumn;
    if( !doc.setContent(&file, &strError, & errLine, &errColumn) )
    {
        file.close();
        creat_xml();
        file.open(QIODevice::ReadOnly);
        if( !doc.setContent(&file, &strError, & errLine, &errColumn) )
        {
            file.close();
            QMessageBox msgBox;
            msgBox.setText("Xml解析错误 : "+strError+" line "+errLine+" col "+errColumn);
            msgBox.exec();
            doc.clear();
            return;
        }
    }
    file.close();
}

void oss_pkg_add::on_xml_add_clicked()
{
    if(doc.isNull()||
            ui->download->text().isNull()||ui->download->text().isEmpty()||
            ui->category->text().isNull()||ui->category->text().isEmpty()||
            ui->name->text().isNull()||ui->name->text().isEmpty()||
            ui->name_code->text().isNull()||ui->name_code->text().isEmpty()||
            ui->zip_size->text().isNull()||ui->zip_size->text().isEmpty())
    {
        return;
    }
    QString url = ui->category->text();
    if(url[0] == QChar('/')) url.remove(0,1);
    if(url[url.length()-1] == QChar('/')) url.remove(url.length()-1,1);
    QStringList urls = url.split(QChar('/'));
    int number = urls.count();
    QDomElement element;
    QDomElement forward = doc.documentElement();
    QDomText text;
    for(int i = 0;i<number;++i)
    {
        element = forward.firstChildElement(urls[i]);
        if(element.isNull())
        {
            element = doc.createElement(urls[i]);
            element.setAttribute("type", "dir");
            forward.appendChild(element);
        }
        forward = element;
    }
    QDomElement pkg;
    pkg = doc.createElement("pkg");
    pkg.setAttribute("type", "pkg");
    forward.appendChild(pkg);
    if(ui->open_url->isChecked())
    {
        //open_url
        element = doc.createElement("open_url");
        pkg.appendChild(element);

        text = doc.createTextNode("true");
        element.appendChild(text);
    }
    else
    {
        //open_url
        element = doc.createElement("open_url");
        pkg.appendChild(element);

        text = doc.createTextNode("false");
        element.appendChild(text);
    }

    //download
    element = doc.createElement("download");
    pkg.appendChild(element);

    text = doc.createTextNode(ui->download->text());
    element.appendChild(text);

    //download_readme
    element = doc.createElement("download_readme");
    pkg.appendChild(element);

    text = doc.createTextNode(ui->readme->text());
    element.appendChild(text);

    //category
    element = doc.createElement("category");
    pkg.appendChild(element);

    text = doc.createTextNode(ui->category->text());
    element.appendChild(text);

    //name
    element = doc.createElement("name");
    pkg.appendChild(element);

    text = doc.createTextNode(ui->name->text());
    element.appendChild(text);

    //name_code
    element = doc.createElement("name_code");
    pkg.appendChild(element);

    text = doc.createTextNode(ui->name_code->text());
    element.appendChild(text);

    //update_code
    element = doc.createElement("update_code");
    pkg.appendChild(element);

    text = doc.createTextNode(ui->update_code->text());
    element.appendChild(text);

    //version
    element = doc.createElement("version");
    pkg.appendChild(element);

    text = doc.createTextNode(ui->ver->text());
    element.appendChild(text);

    //zip_size
    element = doc.createElement("zip_size");
    pkg.appendChild(element);

    text = doc.createTextNode(ui->zip_size->text());
    element.appendChild(text);

    //pkg_size
    if(!(ui->pkg_size->text().isNull()||ui->pkg_size->text().isEmpty()))
    {
        element = doc.createElement("pkg_size");
        pkg.appendChild(element);

        text = doc.createTextNode(ui->pkg_size->text());
        element.appendChild(text);
    }

    //count
    if(!(ui->count->text().isNull()||ui->count->text().isEmpty()))
    {
        element = doc.createElement("count");
        pkg.appendChild(element);

        text = doc.createTextNode(ui->count->text());
        element.appendChild(text);
    }

    //publisher
    if(!(ui->publisher->text().isNull()||ui->publisher->text().isEmpty()))
    {
        element = doc.createElement("publisher");
        pkg.appendChild(element);

        text = doc.createTextNode(ui->publisher->text());
        element.appendChild(text);
    }

    //source
    if(!(ui->source->text().isNull()||ui->source->text().isEmpty()))
    {
        element = doc.createElement("source");
        pkg.appendChild(element);

        text = doc.createTextNode(ui->source->text());
        element.appendChild(text);
    }

    //md5
    if(!(ui->md5->text().isNull()||ui->md5->text().isEmpty()))
    {
        element = doc.createElement("md5");
        pkg.appendChild(element);

        text = doc.createTextNode(ui->md5->text().toUpper());
        element.appendChild(text);
    }

    //type
    element = doc.createElement("type");
    pkg.appendChild(element);

    text = doc.createTextNode(ui->type->currentText().splitRef("(").at(0).toString());
    element.appendChild(text);

    //time
    if(!(ui->time->text().isNull()||ui->time->text().isEmpty()))
    {
        element = doc.createElement("time");
        pkg.appendChild(element);

        text = doc.createTextNode(ui->time->text());
        element.appendChild(text);
    }

    QFile file(ui->xml_file->text());
    if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
    {
        return;
    }
    QTextStream outStream(&file);
    doc.save(outStream,4);
    file.close();
}
